<?php
/**
 * 查询新积分规则和旧规则可用积分是否一直
 * @author yiwan
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Doraemon\model\PointMonth as mPointMonth;
use Medoo\Medoo;

$t1 = microtime(true);

$startDate = '201805';
$endDate = '201903';
$tabIndex = 0;
if (isset($argv[1])) {
    $tabIndex = $argv[1];
}

$tableNum = mPointMonth::_SUB_TABLE_NUM;
if ($tabIndex >= $tableNum) {
    var_dump('不存在tabindex' . $tabIndex);
    die;
} else {
    var_dump(mPointMonth::_TABLE_NAME . $tabIndex . '表开始执行');
    $tabStar = microtime(true);
    mPointMonth::enforceSubTableNameSuffix($tabIndex);
    $uidArr = mPointMonth::select([
        'uid' => Medoo::raw('DISTINCT(uid)'),
    ], ['deduct_point' => 0]);
    foreach ($uidArr as $key=>$row) {
        $uid = $row['uid'];
        $where = [
            'AND' => [
                'uid' => $uid,
                'month[>=]' => $startDate,
                'month[<=]' => $endDate
            ],
            'ORDER' => [
                'month' => 'ASC'
            ]
        ];
        $pointInfo = mPointMonth::getPointCountInfoByUid($uid);
        $point = max(0, $pointInfo['award_point_amount'] - $pointInfo['deduct_point_amount'] - $pointInfo['expiration_point_amount']);
        $currentPoint = mPointMonth::getCurrentPoint($uid);
        if ($point != $currentPoint) {
            var_dump('uid:' . $uid . '的可用积分不正确#current_point:' . $currentPoint . '#point:' . $point);
        }
        if ($point < 0 || $currentPoint < 0) {
            var_dump('uid:' . $uid . '的可用积分小于零#current_point:' . $currentPoint . '#point:' . $point);
        }
        unset($uidArr[$key]);
    }
    $tabEnd = microtime(true);
    $time = $tabEnd - $tabStar;
    var_dump(mPointMonth::_TABLE_NAME . $tabIndex . '表执行完毕。执行时间：' . number_format($time, 2) . 's ');

}


